package com.middlegroup.subjectpay.service.impl;

import com.middlegroup.subjectpay.config.ConstantInfo;
import com.middlegroup.subjectpay.entity.ThirdProtocolApply;
import com.middlegroup.subjectpay.mapper.ThirdProtocolApplyMapper;
import com.middlegroup.subjectpay.service.ThirdProtocolApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 协议支付申请记录表（针对首次支付成功就是绑卡成功的协议支付） 服务实现类
 * </p>
 *
 * @author chengweiwei123
 * @since 2019-01-28
 */
@Service
@Slf4j
public class ThirdProtocolApplyServiceImpl extends ServiceImpl<ThirdProtocolApplyMapper, ThirdProtocolApply> implements ThirdProtocolApplyService {

    @Resource
    private ThirdProtocolApplyMapper thirdProtocolApplyMapper;

    @Autowired
    private ConstantInfo constantInfo;

    @Override
    public ThirdProtocolApply getLastOne(ThirdProtocolApply thirdProtocolApply) {
        try {
            if (thirdProtocolApply != null) {
                thirdProtocolApply.SetEncryptAll(constantInfo.getAeskey());
            }
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        // 查询记录
        ThirdProtocolApply apply = thirdProtocolApplyMapper.getLastOne(thirdProtocolApply);
        try {
            if (apply != null) {
                apply.GetDecryptAll(constantInfo.getAeskey());
            }
        } catch (Exception e) {
            log.error("四要素解密异常：{}", e);
        }
        return thirdProtocolApplyMapper.getLastOne(thirdProtocolApply);
    }

    @Override
    public boolean insert(ThirdProtocolApply entity) {
        try {
            entity.SetEncryptAll(constantInfo.getAeskey());
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        return super.insert(entity);
    }

    @Override
    public boolean updateById(ThirdProtocolApply entity) {
        try {
            entity.SetEncryptAll(constantInfo.getAeskey());
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        return super.updateById(entity);
    }
}
